package com.example.wechat;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private  Fragment setragment=new setragment();
    private  Fragment chatFragment=new chatFragment();
    private  Fragment discoverFragment = new discoverFragment();
    private  Fragment linmanFragment =new linmanFragment();
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private LinearLayout LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
    private TextView  textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout1 = findViewById(R.id.lot1);
        LinearLayout2=findViewById(R.id.lout2);
        LinearLayout3=findViewById(R.id.lout3);
        LinearLayout4=findViewById(R.id.lout4);
        textView=findViewById(R.id.textView0);
        fragmentManager = getSupportFragmentManager();
        initFragment();
        hideFragment();
        LinearLayout1.setOnClickListener( this);
        LinearLayout2.setOnClickListener( this);
        LinearLayout3.setOnClickListener( this);
        LinearLayout4.setOnClickListener( this);
    }

    //选中颜色与标题改变
    private void showcolor(int i) {
        LinearLayout1.setBackgroundColor(Color.WHITE);
        LinearLayout2.setBackgroundColor(Color.WHITE);
        LinearLayout3.setBackgroundColor(Color.WHITE);
        LinearLayout4.setBackgroundColor(Color.WHITE);
        switch (i) {
            case 1:
                LinearLayout1.setBackgroundColor(Color.GREEN);
                textView.setText("聊天");
                break;
            case 2:
                LinearLayout2.setBackgroundColor(Color.GREEN);
                textView.setText("联系人");
                break;
            case 3:
                LinearLayout3.setBackgroundColor(Color.GREEN);
                textView.setText("朋友圈");
                break;
            case 4:
                LinearLayout4.setBackgroundColor(Color.GREEN);
                textView.setText("设置");
                break;
            default:
                break;

        }
    }

    private void initFragment() {
                fragmentManager.beginTransaction();
        fragmentTransaction = fragmentManager.beginTransaction().add(R.id.content,setragment)
                .add(R.id.content,chatFragment).add(R.id.content,discoverFragment).add(R.id.content,linmanFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onClick(View view) {
        int i = 0;
        switch (view.getId()){
            case R.id.lot1:select(1);break;
            case R.id.lout2:select(2);break;
            case R.id.lout3:select(3);break;
            case R.id.lout4:select(4);break;
        }
    }

    public void select(int i){
        hideFragment();
        switch (i){
            case 1:
                showFragment(chatFragment);
                showcolor(1);
                break;
            case 2:
                showFragment(linmanFragment);
                showcolor(2);
                break;
            case 3:
                showFragment(discoverFragment);
                showcolor(3);
                break;
            case 4:
                showFragment(setragment);
                showcolor(4);
                break;
        }
    }

    private void showFragment(Fragment fragment){
        fragmentTransaction.show(fragment);
    }

    //隐藏
    private void hideFragment() {
        fragmentTransaction = fragmentManager.beginTransaction().hide(chatFragment)
                .hide(linmanFragment).hide(discoverFragment).hide(setragment);
        fragmentTransaction.commit();
    }
}